/* ======================================================= */
/**
 * File   : ToolBox.java
 * Date   : 3 mars 2010 at 10:45:18
 */
/* ======================================================= */
package fr.isima.isipaint.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JPanel;

import fr.isima.isipaint.controllers.ToolBoxListener;
import fr.isima.isipaint.model.GraphicObject;
import fr.isima.isipaint.tools.Action;
import fr.isima.isipaint.tools.ToolBox;

/* ======================================================= */
/**
 * 
 * @author Daniel
 * 
 */
/* ======================================================= */
public class ToolBoxPanel extends JPanel {

	/** Serial number */
	private static final long serialVersionUID = 1L;

	/** The buttons tool map */
	private Map<JButton, Action> buttons = new HashMap<JButton, Action>();

	/** The JPanel to display the buttons*/
	private JPanel buttonPanel = new JPanel();
	
	/** The number of columns for the toolBox */
	private static final int NB_COLUMNS = 2;
	
	/** The width of one button*/
	private static final int BUTTON_WIDTH = 30;

	/** The height of one button*/
	private static final int BUTTON_HEIGHT = 30;

	/** The tools set */
	private ToolBox tools;
	
	/** The default Listener*/
	private ToolBoxListener listener;

	/**
	 * Default Constructor
	 */
	public ToolBoxPanel(ToolBox toolBox, GraphicObject current) {
		this.tools = toolBox;
		listener = new ToolBoxListener(current);
		
		/* To store the mapping between buttons and actions*/
		for (Action t : tools.getTools()) {
			JButton button = new JButton();
			if(t.isEnableIcon() && t.getIcon() != null)
			{
				button.setIcon(t.getIcon());
			}
			else
			{
				button.setText(t.getName());
			}
			button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
			button.addActionListener(listener);
			buttons.put(button, t);
		}

		/* Jpanel for buttons */
		add(buttonPanel);
		buttonPanel.setLayout(new GridBagLayout());
		 GridBagConstraints constraints = new GridBagConstraints();
				
		int i = 0;
		int j = -1;

		for (Entry<JButton, Action> entry : buttons.entrySet()) {
			constraints.gridx = i++ % NB_COLUMNS;
			constraints.gridy = (i == 0) ? j++ : j;
			buttonPanel.add(entry.getKey(), constraints);
		}
	}

}
